//
//  AIMyLibraryCenterCell.m
//  HNBaseProject
//
//  Created by xujie on 2024/9/15.
//

#import "AIMyLibraryCenterCell.h"
#import "AIMyLibraryShowCell.h"
#import "AIMyHistoryCreateModel.h"
@implementation AIMyLibraryCenterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}

- (void)createUI {
    self.listArray = [[NSMutableArray alloc] init];
    self.backgroundColor = [UIColor colorWithHexString:@"#F5F8FB"];
    [self.contentView addSubview:self.listCV];
    [self.listCV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.top.bottom.offset(0);
    }];

    self.nodataLab1 = [[UILabel alloc] init];
    self.nodataLab1.hidden = YES;
    self.nodataLab1.text = NSLocalizedString(@"暂无作品", nil);// @"No projects yet";
    [self.contentView addSubview:self.nodataLab1];
    self.nodataLab1.textColor = [UIColor colorWithHexString:@"#000000"];
    self.nodataLab1.font = kFontBold(18);
    [self.nodataLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(193);
        make.centerX.mas_equalTo(self.contentView);
    }];
    
    self.nodataLab2 = [[UILabel alloc] init];
    self.nodataLab2.text = NSLocalizedString(@"作品会在这里展示", nil);//@"Projects will be listed here";
    [self.contentView addSubview:self.nodataLab2];
    self.nodataLab2.textColor = [UIColor colorWithHexString:@"#666666"];
    self.nodataLab2.font = kFontRegular(14);
    [self.nodataLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nodataLab1.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.contentView);
    }];
    self.nodataLab2.hidden = YES;
    
}

- (UICollectionView *)listCV {
   if (!_listCV) {
       UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
       layout.itemSize = CGSizeMake((kScreenW - 45) / 2.0, 165);
       layout.minimumLineSpacing = 15;//纵向间距(参考scrollDirection)
       layout.minimumInteritemSpacing = 15;//横向间距
       layout.scrollDirection = UICollectionViewScrollDirectionVertical;
       _listCV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
       _listCV.backgroundColor = [UIColor clearColor];
       _listCV.delegate = self;
       _listCV.dataSource = self;
       _listCV.showsVerticalScrollIndicator = NO;
       _listCV.showsHorizontalScrollIndicator = NO;
       [_listCV registerClass:[AIMyLibraryShowCell class] forCellWithReuseIdentifier:@"AIMyLibraryShowCell"];
       
       }
    return _listCV;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 4;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        AIMyLibraryShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AIMyLibraryShowCell" forIndexPath:indexPath];
        if (self.listArray.count > 0) {
            AIMyHistoryCreateModel *model = self.listArray[0];
            [cell reloadCellWithModel:model];
        } else {
            [cell reloadEmptyCell];
        }
        return cell;
    } else if (indexPath.row == 1) {
        AIMyLibraryShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AIMyLibraryShowCell" forIndexPath:indexPath];
        if (self.listArray.count > 1) {
            AIMyHistoryCreateModel *model = self.listArray[1];
            [cell reloadCellWithModel:model];
        } else {
            [cell reloadEmptyCell];
        }
        return cell;
    } else if (indexPath.row == 2) {
        AIMyLibraryShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AIMyLibraryShowCell" forIndexPath:indexPath];
        if (self.listArray.count > 2) {
            AIMyHistoryCreateModel *model = self.listArray[2];
            [cell reloadCellWithModel:model];
        } else {
            [cell reloadEmptyCell];
        }
        return cell;
    }
    AIMyLibraryShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AIMyLibraryShowCell" forIndexPath:indexPath];
    if (self.listArray.count > 3) {
        AIMyHistoryCreateModel *model = self.listArray[3];
        [cell reloadCellWithModel:model];
    } else {
        [cell reloadEmptyCell];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellClick) {
        self.cellClick(indexPath.item);
    }
}

- (void)reloadCellWithArray:(NSMutableArray *)dataArray {
    self.listArray = dataArray;
    self.nodataLab1.hidden = dataArray.count >0;
    self.nodataLab2.hidden = dataArray.count >0;
    [self.listCV reloadData];
}

@end
